
  
function DoAction(id, slde, sltrungbinh, slkho, thoigian)
{
   
    $("#idmucdo").val(id);
    $("#idde").val(slde);
    $("#idtrungbinh").val(sltrungbinh);
    $("#idkho").val(slkho);
    $("#idthoigian").val(thoigian);

     $( "#frm-edit" ).dialog("open");

}
                
$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
                

                
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		 var  tips = $( ".validateTips" );
		

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function KiemTraCauHoi( s ) {
                      
			if ( s.length == 0 ) {
                                
				updateTips("Bạn chưa điền NỘI DUNG CÂU HỎI");
				return false;
			} else {
				return true;
			}
		}
                
                function KiemTraCauTraLoi( s,i ) {
                   
			if ( s.length == 0 ) {
				
				updateTips("Bạn chưa điền nội dung câu trả lời thứ: " + i);
				return false;
			} else {
				return true;
			}
		}

		function KiemTraCauradionbuton( s ) {
                   
			if ( s.length ==1 ) {
				
                             return true;
				
			} else {
                            updateTips("Bạn chưa ĐÁP ÁN ĐÚNG");
				return false;
				
                        }
                        
		}
               
               function KiemTraUpload( s ) {
                      
			if ( s.length == 0 ) {
                                
				updateTips("Bạn chưa chọn ");
				return false;
			} else {
				return true;
			}
		}
                
                 function KiemTraLoaiFile( s ) {
                      
			if ( s!="xml" ) {
                                
				updateTips("Bạn phải chọn loại file là xml");
				return false;
			} else {
				return true;
			}
		}
                
                function la_so(n)
                {
                    if (isNaN(Number(n)))
                        {
                           updateTips("Bạn phải nhập số, không được nhập chữ"); 
                           return false;
                        }
                       
                        
                    else
                        return true;

                }
                function kientrathoigian(thoigian)
                {
                    var thoigian =  parseInt(thoigian);
                    if(thoigian>120)
                    {
                         $( "#loithoigian" ).dialog({
                                                        modal: true,
                                                        autoOpen: true,
                                                        buttons: {
                                                                OK: function() {
                                                                        $( this ).dialog( "close" );
                                                                }
                                                        }
                                                    });
                       ;    
                        return false;
                    }
                    return true;

                }
                function kiemtratongcau(de,kho,trungbinh)
                {
                    var slde =  parseInt(de);
                    var slkho =  parseInt(kho);
                    var sltrungbinh =  parseInt(trungbinh);
                    
                    
                    var tongcau = slde + slkho + sltrungbinh;
                    
                    if(tongcau>60)
                    {
                        
                        $( "#loiqua60cau" ).dialog({
                                                        modal: true,
                                                        autoOpen: true,
                                                        buttons: {
                                                                OK: function() {
                                                                        $( this ).dialog( "close" );
                                                                }
                                                        }
                                                    });
                        return flase;                            
                    } 
                    if(tongcau<60)
                        {
                            $( "#loithieu60cau" ).dialog({
                                                        modal: true,
                                                        autoOpen: true,
                                                        buttons: {
                                                                OK: function() {
                                                                        $( this ).dialog( "close" );
                                                                }
                                                        }
                                                    });
                        return flase;     
                        
                        
                        }
                    return true;
                }
                $( "#frm-edit" ).dialog({
			autoOpen: false,
			height: 350,
			width: 500,
			modal: true,
			buttons: {
				"Update": function() {
					var bValid = true;
                                       
                                        var slde = $("#idde").val();
                                        var sltrungbinh  = $("#idtrungbinh").val();
                                        var slkho = $("#idkho").val();
                                        var thoigian = $("#idthoigian").val();
                                        
                                        bValid = bValid && la_so(slde);
                                        bValid = bValid && la_so(sltrungbinh);
                                        bValid = bValid && la_so(slkho);
                                        bValid = bValid && la_so(thoigian);
                                        bValid = bValid && kiemtratongcau(slde, slkho, sltrungbinh)
                                        bValid = bValid && kientrathoigian(thoigian);
					if ( bValid ) {	
                                                  
                                            $("#form2").submit();
					}
				},
				"Cancel": function() {
					$( this ).dialog("close");
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
                
                $( "#frm-personal-info111" ).dialog({
			autoOpen: false,
			height: 550,
			width: 800,
			modal: true,
			buttons: {
				"Insert": function() {
					var bValid = true;
					
					
                                        var cauhoi = $("#cauhoi").val();
                                        var traloi1 = $("#answer_1").val();
                                        var traloi2 = $("#answer_2").val();
                                        var traloi3 = $("#answer_3").val();
                                        var traloi4 = $("#answer_4").val();
                                       
                                        var dokho = $("#comboboxLevel").val();
                                        var dapan = $("input[name='radio']:checked").val();
                                        bValid = bValid && KiemTraCauHoi(cauhoi);
                                        bValid = bValid && KiemTraCauTraLoi(traloi1,1);
                                        bValid = bValid && KiemTraCauTraLoi(traloi2,2);
                                        bValid = bValid && KiemTraCauTraLoi(traloi3,3);
                                        bValid = bValid && KiemTraCauTraLoi(traloi4,4);                                       
                                        bValid = bValid && KiemTraCauradionbuton(dapan);                                       
					if ( bValid ) {	
                                                  
                                            $.ajax({
                                                type: "POST",
                                                url: 'SoanCauHoi?Action=insert',
                                                data: "cauhoi="+cauhoi+"&traloi1="+traloi1 +"&traloi2=" +traloi2 + "&traloi3=" +traloi3 +"&traloi4=" +traloi4 +"&dokho=" +dokho,
                                                dataType: 'text',
                                                success:function(msg){ 
                                                    $( "#them-success" ).dialog({
                                                        modal: true,
                                                        autoOpen: true,
                                                        buttons: {
                                                                OK: function() {
                                                                        $( this ).dialog( "close" );
                                                                }
                                                        }
                                                    });
                                                },
                                                error: function (){
                                                    updateTips('Insert thất bại');
                                                }
                                           });	
					}
				},
				"Cancel": function() {
					$( this ).dialog("close");
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
              
		$( "#start111" )
			.click(function() {
                            
                            $( "#frm-personal-info111" ).dialog( "open" );        
			});  
                 $( "#frm-iduploadQuestion" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				"Insert": function() {
                                      
					var bValid = true;
					var file = $("#file").val();
                                        bValid = bValid && KiemTraUpload(file); 
                                        if(bValid)
                                        {
                                            var element = $("#file").val().split('.');                                                                                       
                                            bValid = bValid && KiemTraLoaiFile(element[1]);                                            
                                        }
  
                                      
					if ( bValid ) {	                               
                                            
                                            $("#form1").submit();  
                                             
					}
				},
				"Cancel": function() {
					$( this ).dialog("close");
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
                 $("#idupload")
			.click(function() {                            
                            $( "#frm-iduploadQuestion" ).dialog( "open" );        
			});  
                        
                        
                        
                  $( "#frm-thongke" ).dialog({
			autoOpen: false,
			height: 250,
			width: 500,
			modal: true,
			buttons: {
				
				"Cancel": function() {
					$( this ).dialog("close");
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});      
                  $("#idthongke")
                    .click(function() {                            
                        $( "#frm-thongke" ).dialog( "open" );        
                    }); 
	});
  
  ////////////////////////combobox//////////	
(function( $ ) {
		$.widget( "ui.combobox", {
			_create: function() {
				
				var self = this,
					select = this.element.hide(),
					selected = select.children( ":selected" ),	
					value = selected.val() ? selected.text() : "";
				var input = this.input = $( "<input>" )
					.insertAfter( select )
					.val( value )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
							response( select.children( "option" ).map(function() {
								var text = $( this ).text();
								if ( this.value && ( !request.term || matcher.test(text) ) )
									return {
										label: text.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
										value: text,
										option: this
									};
							}) );
						},
					
						select: function( event, ui ) {
							
							
							ui.item.option.selected = true;
							self._trigger( "selected", event, {
								item: ui.item.option
							});

						},
						change: function( event, ui ) {
							
							if ( !ui.item ) {
								var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
									valid = false;
								select.children( "option" ).each(function() {
									if ( $( this ).text().match( matcher ) ) {
										this.selected = valid = true;
										return false;
									}
								});
								if ( !valid ) {
									// remove invalid value, as it didn't match anything
									
									$( this ).val( "" );
									select.val( "" );
									input.data( "autocomplete" ).term = "";
									return false;
								}
							}
						}
					})
					.addClass( "ui-widget ui-widget-content ui-corner-left" );
			
				input.data( "autocomplete" )._renderItem = function( ul, item ) {
					return $( "<li></li>" )
						.data( "item.autocomplete", item )
						.append( "<a>" + item.label + "</a>" )
						.appendTo( ul );
				};

				this.button = $( "<button type='button'>&nbsp;</button>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "Show All Items" )
					.insertAfter( input )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "ui-corner-right ui-button-icon" )
					.click(function() {
						// close if already visible

						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
							input.autocomplete( "close" );
							return;
						}

						// work around a bug (likely same cause as #5265)
						$( this ).blur();

						// pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
						input.focus();
					});
			},

			destroy: function() {
				this.input.remove();
				this.button.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
	})( jQuery );

	$(function() {
		$( "#comboboxLevel" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#comboboxLevel" ).toggle();
		});
                
               
	}); 